package com.zeng.incast.videoplay.live;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.zeng.incast.videoplay.R;
import com.zeng.incast.videoplay.hardcode.VideoWrapper;

public class LiveActivity extends AppCompatActivity {
    static {
        System.loadLibrary("camera");
    }

    private String TAG = "LiveActivity";

    private camera mCamera;

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private Surface mSurface;

    private boolean mIsSupportAvc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.live);

        initData();
        initView();
        initCamera();
        initHardCode();
    }

    private void initData() {
        mIsSupportAvc = AvcHardCodeIsSupport();
    }

    private void initView() {
        mSurfaceView = (SurfaceView) findViewById(R.id.camera_surfaceview);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurface = mSurfaceHolder.getSurface();
    }

    private void initCamera() {
        final SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mCamera = new camera();
                mCamera.init("rtsp://192.168.100.118:5000/sub.h264", "admin", "admin", 0);

                mSurfaceHolder = holder;
                mSurface = mSurfaceHolder.getSurface();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                mSurfaceHolder = holder;
                mSurface = mSurfaceHolder.getSurface();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mCamera.exit();
            }
        });
    }

    private boolean AvcHardCodeIsSupport() {
        if (Build.VERSION.SDK_INT >= 18) {
            for (int i = 0; i < MediaCodecList.getCodecCount(); i++) {
                MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
                String[] types = codecInfo.getSupportedTypes();
                for (int j = 0; j < types.length; j++) {
                    if (types[j].equalsIgnoreCase("video/avc")) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    private void initHardCode() {
        VideoWrapper videoWrapper = VideoWrapper.getInstance();
        videoWrapper.setSurface(mSurface);
        videoWrapper.startDecode();
    }

    private void exitHardCode() {
        VideoWrapper videoWrapper = VideoWrapper.getInstance();
        videoWrapper.stopDecode();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: !!! Live\n");
        exitHardCode();
    }
}
